﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.Collapse
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TableFieldValueDistributionComponent:WorkflowComponentBase
    {
        #region props
        private TableFieldValueDistributionInstruction _Instruction;
        private TableFieldValueDistributionOutput _Output;

        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction =(TableFieldValueDistributionInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output =(TableFieldValueDistributionOutput) value; }
        }

        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._Instruction.ConnStr;
            }
            set
            {
                this._Instruction.ConnStr = value;
            }
        }

        /// <summary></summary>
        public string TableName
        {
            get
            {
                return this._Instruction.TableName;
            }
            set
            {
                this._Instruction.TableName = value;
            }
        }

        /// <summary></summary>
        public string FieldName
        {
            get
            {
                return this._Instruction.FieldName;
            }
            set
            {
                this._Instruction.FieldName = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public TableFieldValueDistributionComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.TextMining.ToString();
            this.ComponentName = "Field value distribution";
            this.Description = "Evaluate table field value distribution statistics (such as spread, mode etc)";
            this.ComponentIcon = Resources.calc_SC;
            this.ComponentImage = Resources.calc;
            
            this._Instruction=new TableFieldValueDistributionInstruction(this.ComponentID,Guid.Empty,string.Empty,string.Empty,string.Empty);
            this._Output = new TableFieldValueDistributionOutput(this.ComponentID, Guid.Empty, string.Empty,
                                                                 string.Empty, SqlDbType.VarChar,
                                                                 new Dictionary<string, int>(),
                                                                 new List<ModePercentile>(), 0);
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FieldName", this.FieldName);
            
            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                TableFieldValueDistributionComponent component =
                    base.Instantiate(xNode) as TableFieldValueDistributionComponent;

                component.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                component.TableName = XmlDataUtil.GetAttributeValue(xNode, "TableName", "");
                component.FieldName = XmlDataUtil.GetAttributeValue(xNode, "FieldName", "");

                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate email: invalid type specified in xml node");
            }
        }
    }
}
